﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manageweb_Stock_ManageWebSiteConfig : System.Web.UI.Page
{
    private const string _ConfigName_DealPrice = "StockDealPrice";
    private const string _ConfigName_MemAccountMoney = "MemAccountMoney";
    private const string _ConfigName_DealRule = "DealRule";
    private const string _ConfigName_DeductMoney = "DeductMoney";
    private const string _ConfigName_AddValue = "AddValue";
    private const string _ConfigName_AB1 = "AB1";
    private const string _ConfigName_AB2 = "AB2";
    private const string _ConfigName_AB3 = "AB3";

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void LBtn_Mod_Command(object sender, CommandEventArgs e)
    {
        String[] configInfo = e.CommandArgument.ToString().Split(new char[] { '|' });

        this.txtConfigID.Text = configInfo[0];
        this.txtConfigName.Text = configInfo[3];
        this.txtConfigValue.Text = configInfo[2];

        this.hidConfigName.Value = configInfo[1];

        GetValidatorRule(configInfo[1]);

        this.TABLE_Add_Mod.Visible = true;
    }

    protected void GetValidatorRule(string configname)
    {
        string rule = "";

        switch (configname)
        {
            case _ConfigName_DealPrice:
            case _ConfigName_DeductMoney:
            case _ConfigName_AddValue:
                rule = @"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$";
                break;
            case _ConfigName_AB1:
            case _ConfigName_AB2:
            case _ConfigName_AB3:
      // rule = @"^0[1-9]$|^[1-9][0-9]?$|^00?\.(?:0[1-9]|[1-9][0-9]?)$|^(?:0[1-9]|[1-9][0-9]?)\.[0-9]$";
                rule = @"^(0([\.]\d*[0-9]+)|0|1)$";
                this.RegularExpressionValidator1.ErrorMessage = "请输入0~1之间的小数";
                break;
            case _ConfigName_DealRule:
            case _ConfigName_MemAccountMoney:
                rule = "^[0-9]*[1-9][0-9]*$";
                this.RegularExpressionValidator1.ErrorMessage = "正整数";
                break;

        }

        this.RegularExpressionValidator1.ValidationExpression = rule;

    }


    protected void Btn_Submit_Click(object sender, EventArgs e)
    {
        string msg = "修改失败.";

        try
        {
            string configValue = this.txtConfigValue.Text.Trim();
            Int16 configID = Convert.ToInt16(this.txtConfigID.Text.Trim());

            switch (this.hidConfigName.Value)
            {
                case _ConfigName_AB1:
                case _ConfigName_AB2:
                case _ConfigName_AB3:
                    if (!CheckAccountPercent(this.hidConfigName.Value, configValue))
                    {
                        msg = "百分比设置错误!";
                        return;
                    }
                    break;            
            }          


            SQLExpressDAL.DealRuleTableAdapters.WebSiteConfigTableAdapter adp = new SQLExpressDAL.DealRuleTableAdapters.WebSiteConfigTableAdapter();
            int flag = adp.UpdateConfigValue(configValue,configID);

            if (flag > 0)
            {
                msg = "修改成功.";
            }

        }
        catch (Exception ex)
        {
            msg = "修改失败.\n" + ex.Message;
        }
        finally
        {
            this.LB_ManageUser.Text = msg;
            this.TABLE_Add_Mod.Visible = false;
            ClearTxt();
            this.GV_UserList.DataBind();
        }

    }
    /// <summary>
    /// 检查账户所占百分比是否为100%
    /// </summary>
    /// <returns></returns>
    protected bool CheckAccountPercent(string currAccountCode, string currAccountValue)
    {
        bool flag = true;

        SQLExpressDAL.DealRuleTableAdapters.WebSiteConfigTableAdapter adp = new SQLExpressDAL.DealRuleTableAdapters.WebSiteConfigTableAdapter();
        decimal percent = Convert.ToDecimal(adp.GetOtherAccountPercent(currAccountCode));

        if (currAccountValue.Equals("0"))
        {
            return flag;
        }

        if (Convert.ToDecimal(currAccountValue) > percent)
        {
            flag = false;

        } 

        return flag;

    }

    protected void ClearTxt()
    {
        this.txtConfigID.Text = "";
        this.txtConfigName.Text = "";
        this.txtConfigValue.Text = "";
    }
}
